/**
 * Created by LeungZ on 2016/8/10.
 */

import alertTmp from '../form/alert.html';
import alertCtrl from '../form/alert.ctrl.js';

class Detail {
    constructor(detail, $stateParams, appSrv, ctgNotification, services, ctgColor, $uiModal) {
        "ngInject";
        this._notification = ctgNotification;
        this._uiModal = $uiModal;
        this.id = $stateParams.id;
        this._appSrv = appSrv;
        this._color = ctgColor;
        this.edition = false;

        if (detail) {
            this.appCode = detail.appCode;
            this.appName = detail.appName;
            this.content = detail.description;
            this.k8sCode = detail.k8sCode;
            this.namespace=detail.namespace;
        }
// console.log(this.namespace)
        this.page = {
            currentPage: 1,
            totalItems: 0
        };

        if (services) {
            this.serviceList = services.list;
            angular.forEach(services.list, ser=> {
                ser.appId = detail.appId;
                ser.currentPage = 1;
                ser.pageSize = 3;
                ser.border = {'border-left': '2px groove ' + this._color[ser.serviceId % this._color.length]};
                ser.color = {'color': this._color[ser.serviceId % this._color.length]};
            });
            this.page.totalItems = services.total;
        } else this.serviceList = [];
    }

    getList() {
        this.serviceList = null;
        this._appSrv.getServiceOfApp({id: this.id, currentPage: this.page.currentPage}).then(data=> {
            this.serviceList = data.list;
            this.page.totalItems = data.total;
            angular.forEach(this.serviceList, ser=> {
                ser.appId = this.id;
                ser.currentPage = 1;
                ser.pageSize = 3;
                ser.border = {'border-left': '2px groove ' + this._color[ser.serviceId % this._color.length]};
                ser.color = {'color': this._color[ser.serviceId % this._color.length]};
            });
            console.log(this.page.totalItems,'serverlist');
        }).catch(data=> {
            this.serviceList = [];
            this._notification.error('获取应用服务列表数据有误.');
        });
    }

    deleteService(elem){
        this._uiModal.open({
            templateUrl: alertTmp,
            controller: alertCtrl,
            controllerAs: 'vm',
            windowClass: 'alert-dialog alert-dialog-ctg',
            resolve: {
                alert: function () {
                    return {
                        appCode: elem.serviceCode,
                        action: '移除'
                    }
                }
            }
        }).result.then(()=> {
                elem.deleting = true;
                this._appSrv.deleteService(elem.serviceId).then(data=> {
                    this._notification.success('移除服务成功.');
                    if (this.serviceList.length == 1 && this.page.currentPage > 1)  this.page.currentPage--;
                    this.getList();
                }).catch(data=> {
                    this._notification.error('移除服务失败. ' + data.resultMsg);
                }).finally(()=>{
                    elem.deleting = false;
                })
            });
    }

    editDetail() {
        this.content_edit = this.content;
        this.edition = true;
    }

    cancelEdit() {
        this.edition = false;
    }

    saveEdit() {
        this.edition = "loading";
        this._appSrv.update({id: this.id, content: this.content_edit}).then(()=> {
            this.content = this.content_edit;
            this.edition = false;
            this._notification.success('更新应用详情成功.');
        }).catch(data=> {
            this.edition = true;
            this._notification.error('更新应用详情失败.');
        })
    }
}
export default Detail